package cn.zhf.shop.utils;

import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class PayUtil {

    /**
     * 支付宝H5支付地址
     */
    @Value("${Pay.H5_PAY_URL}")
    public static final String H5_PAY_URL = "https://api-mop.chinaums.com/v1/netpay/trade/h5-pay";

    /**
     * 微信H5支付地址
     */
    @Value("${Pay.WX_PAY_URL}")
    public static final String WX_PAY_URL = "https://api-mop.chinaums.com/v1/netpay/wxpay/h5-pay";

    /**
     * 银联云闪付支付地址
     */
    @Value("${Pay.UAC_PAY_URL}")
    public static final String UAC_PAY_URL = "https://api-mop.chinaums.com/v1/netpay/uac/order";

    /**
     * APPID 银联商务给的 AppId
     */
    @Value("${Pay.APPID}")
    public static final String APPID = "8a81c1be7d3411ad017ddb0433070622";

    /**
     * APPKEY 银联商务给的加密
     */
    @Value("${Pay.APPKEY}")
    public static final String APPKEY = "1c39d595abaf4d60ad01a24fc5780a63";

    /**
     * MID 临时Mid
     */
    @Value("${Pay.MID}")
    public static final String MID = "89810004816013P";

    /**
     * 临时Tid
     */
    @Value("${Pay.TID}")
    public static final String TID = "ZHFK0003";

    /**
     * MID 商户号 三期
     */
    @Value("${Pay.MID_3}")
    public static final String MID_3 = "898100048161A6Y";

    /**
     * 终端号 三期
     */
    @Value("${Pay.TID_3}")
    public static final String TID_3 = "DM103576";

    /**
     * MID 商户号 六期
     */
    @Value("${Pay.MID_6}")
    public static final String MID_6 = "898100048161A6W";

    /**
     * 终端号 六期
     */
    @Value("${Pay.TID_6}")
    public static final String TID_6 = "DM111436";

    /**
     * MID 商户号 十二期
     */
    @Value("${Pay.MID_12}")
    public static final String MID_12 = "898100048161A6X";

    /**
     * 终端号 十二期
     */
    @Value("${Pay.TID_12}")
    public static final String TID_12 = "DM098832";

    /**
     * MID 商户号 二十四期
     */
    @Value("${Pay.MID_24}")
    public static final String MID_24 = "898100048161A6Z";

    /**
     * 终端号 二十四期
     */
    @Value("${Pay.TID_24}")
    public static final String TID_24 = "DM098831";

    /**
     * MID 商户号 分账子商户号1
     */
    @Value("${Pay.MID_fenzhang1}")
    public static final String MID_fenzhang1 = "89810004816013R";

    /**
     * 终端号 分账子商户1
     */
    @Value("${Pay.TID_fenzhang1}")
    public static final String TID_fenzhang1 = "ZHFK0001";

    /**
     * MID 商户号 分账子商户号2
     */
    @Value("${Pay.MID_fenzhang2}")
    public static final String MID_fenzhang2 = "89810004816013P";

    /**
     * 终端号 分账子商户2
     */
    @Value("${Pay.TID_fenzhang2}")
    public static final String TID_fenzhang2 = "ZHFK0003";

    /**
     * INST_MID 业务类型
     */
    @Value("${Pay.INST_MID}")
    public static final String INST_MID = "H5DEFAULT";

    /**
     * 时间戳日期格式 yyyyMMddHHmmss
     */
    @Value("${Pay.FORMAT_FOURTEEN}")
    public static final SimpleDateFormat FORMAT_FOURTEEN = new SimpleDateFormat("yyyyMMddHHmmss");

    /**
     * 时间日期格式  yyyy-MM-dd HH:mm:ss
     */
    @Value("${Pay.FORMAT_NINETEEN}")
    public static final SimpleDateFormat FORMAT_NINETEEN = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 前台支付返回地址
     */
    @Value("${Pay.MerPageUrl}")
    public static String MerPageUrl = "https://qiudaokeji.cn/zhihuifu/#/";                                      //用来接收交易结果的前台跳转页面的地址，用于引导付款人支付后返回商户网站页面

    /**
     * 获取随机数
     * @return 返回六位随机数 String
     */
    public static String getNonce(){
        Random rand = new Random();
        Integer randNumber =rand.nextInt(900000) + 100000;
        return randNumber.toString();
    }

    /**
     * 获取时间戳
     * @return 返回 yyyyMMddHHmmss String
     */
    public static String getTimestamp(){
        return PayUtil.FORMAT_FOURTEEN.format(new Date());
    }

    /**
     * 获取时间
     * @return 返回 yyyy-MM-dd HH:mm:ss String
     */
    public static String getTime(){
        return PayUtil.FORMAT_NINETEEN.format(new Date());
    }

    /**
     * sha256hex 加密
     * @return 返回 String
     */
    public static String sha256Hex(String content){
        return DigestUtils.sha256Hex(content);
    }

    /**
     * sha256 加密
     * @return 返回 String
     */
    public static byte[] sha256(String content){
        return DigestUtils.sha256(content);
    }

    /**
     * Hmac_sha256 加密
     * @param message 需要加密的内容
     * @param secret 机密的key
     * @return
     */
    public static String Hmac_sha256(String message, String secret){
        String hash = "";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            hash= Base64.encodeBase64String(sha256_HMAC.doFinal(message.getBytes()));
        }
        catch (Exception e){
            System.out.println("Hmac_sha256 error  => " + e.getMessage());
        }
        finally {
            return hash;
        }
    }

    /**
     * 发送post请求
     * @param url 发送请求的url地址
     * @param json 要发送的内容
     * @param header 设置的header 设请写 null/“”
     * @return
     */
    public static JSONObject doPost(String url, JSONObject json, String header){
        CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost(url);
        JSONObject response = null;
        try {
            StringEntity s = new StringEntity(json.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            post.setEntity(s);
            if(header != null && !"".equals(header)){
                post.setHeader("Authorization", header);
            }
            CloseableHttpResponse res = httpclient.execute(post);
            HttpEntity entity = res.getEntity();
            String result = EntityUtils.toString(entity);
            response = JSONObject.fromObject(result);
            res.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return response;
    }

}
